package form4;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.form.DropDownChoice;

import FMS.Form4;
import FMS.Form4_1;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;



public class Form4Sub extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model orNoModel = new Model();
	private Model particularsModel = new Model();
	private Model amountModel = new Model();
	private Model dateDisbursedModel = new Model();
	private Model liquidationFormNoIdsModel = new Model();
	private DropDownChoice liquidationFormNoIds;
	
public Form4Sub() throws IOException, SQLException {
	
	    List<Form4> list = (List<Form4>)sqlMap.queryForList("Form4.getLiquidationFormNoId");
	
	    ListView eachLiquidationFormNoId = new ListView("eachLiquidationFormNoId", list) {
		      private static final long serialVersionUID = 1L;
		
		      protected void populateItem(ListItem item) {
		    	  final Form4 form4 = (Form4) item.getModelObject(); 
		    	  item.add(new DropDownChoice("liquidationFormNoIds", liquidationFormNoIdsModel, Arrays.asList(form4.getLiquidationFormNoId())));
			
		}			
	};
		
		Form form = new Form("form4Sub");
		
		TextField orNo = new TextField("orNo", orNoModel);
		orNo.setRequired(true);
		form.add(orNo);
		
		TextField  particulars = new TextField("particulars", particularsModel);
		particulars.setRequired(true);
		form.add(particulars);
		
		TextField  amount= new TextField("amount", amountModel);
		amount.setRequired(true);
		form.add(amount);
		
		TextField  dateDisbursed = new TextField("dateDisbursed", dateDisbursedModel);
		dateDisbursed.setRequired(true);
		form.add(dateDisbursed);
		
form.add(new Button("add"){
			
			private static final long serialVersionUID = 1L;
		
				public void onSubmit(){
				
					try {
				
						String eachLiquidationFormNoId = (String)liquidationFormNoIdsModel.getObject();
						String dateDisbursed = (String)dateDisbursedModel.getObject();
						String orNo = (String)orNoModel.getObject();
						String particulars = (String)particularsModel.getObject();
						String amount = (String)amountModel.getObject();
						
						
						Form4_1 form4_1 = new Form4_1();
						
						//form4_1.setLiquidationFormNo(liquidationFormNo);
						form4_1.setOrNo(orNo);
						form4_1.setParticulars(particulars);	
						form4_1.setAmount(amount);
						form4_1.setDateDisbursed(dateDisbursed);
						
												
						
						sqlMap.insert("Form4_1.insert", form4_1);
						
						ViewForm4_1 page = new ViewForm4_1();
						setResponsePage(page);
						error("added");
					}
					catch (Exception exception){
					 	error("Fill up necessary fields");
				 	        exception.printStackTrace();}
				//}catch (IOException ioException) {
					//ioException.printStackTrace(); }
					
				}}); 

   form.add(eachLiquidationFormNoId);
   add(form);
		
}}
	
	
	
	